package org.itboys.ace.web;

import org.itboys.admin.dto.AdminUserData;
import org.itboys.admin.entity.GlobalConfigDB;
import org.itboys.admin.service.GlobalConfigService;
import org.itboys.admin.tools.AdminCookieHolder;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.seller.constant.GlobalConfigConstants;
import org.itboys.seller.entity.SiteUser;
import org.itboys.seller.meta.Account;
import org.itboys.seller.service.AccountService;
import org.itboys.seller.service.CaliforniaSiteUserService;
import org.itboys.seller.service.InfoPublishService;
import org.itboys.seller.service.SellerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页相关
 * 
 * @author 伟哥好强
 * 
 */
@RestController
public class SellerAdminIndexController extends BaseController {

	@Autowired
	private SellerUserService sellerUserService;
	@Autowired
	private ResourceHolder resourceHolder;
    @Resource
    private InfoPublishService infoPublishService;
	@Resource
	private AccountService accountService;
	@Resource
	private CaliforniaSiteUserService californiaSiteUserService;
	@Resource
	private GlobalConfigService globalConfigService;

	@RequestMapping("/getAdminUserData")
	public AdminUserData getAdminUserData() {
		AdminUserData userData = new AdminUserData();
		Long userId = AdminSessionHolder.getAdminUserId();
		if (userId == null) {
			userData.setLogined(false);
			return userData;
		}
		userData = sellerUserService.installAdminUserData(userId);

		return userData;
	}

	/**
	 * 首页跳转
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = { "/", "" })
	public ModelAndView index(HttpServletResponse response, Model model)
			throws Exception {
		
		/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
		
		if (AdminSessionHolder.getAdminUserId() == null) {
			return new ModelAndView("redirect:/site/login");
			// response.sendRedirect(resourceHolder.getStringValue("adminLoginUrl"));
		} else {
            String accountId = AdminCookieHolder.getAdminUserName();
            model.addAttribute("accountId",accountId);
            model.addAttribute("totalMatch", Math.ceil(infoPublishService.totalInfo(accountId) / 2));

			long userId = Long.valueOf(AdminCookieHolder.getAdminUserId());
			SiteUser siteUser = californiaSiteUserService.getAdminUserByUserId(userId);
			SiteUser resUser = californiaSiteUserService.getAdminUserByUserId(siteUser.getRecUserId());
			Account account = accountService.getById(userId);

			List<Account> rankAccounts = accountService.batchQueryRankAccount();
			int rank = 0;
			for (int i = 0; i < rankAccounts.size(); i ++) {
				if (rankAccounts.get(i).getUserId() == userId) {
					rank = i + 1;
				}
			}
			model.addAttribute("siteUser",siteUser);
			model.addAttribute("resUser",resUser);
			model.addAttribute("amount",account.getAmount());
			model.addAttribute("recAmount",account.getRecentAmount());
			model.addAttribute("score",account.getScore());
			model.addAttribute("recentScore",account.getRecentScore());
			model.addAttribute("rank",rank);
            model.addAttribute("recentSScore",account.getRecentScoreS());
            model.addAttribute("recentSSScore",account.getRecentScoreSs());
            model.addAttribute("rank",rank);
            rankAccounts = accountService.batchQuerySRankAccount();
            rank = 0;
            for (int i = 0; i < rankAccounts.size(); i ++) {
                if (rankAccounts.get(i).getUserId() == userId) {
                    rank = i + 1;
                }
            }
            model.addAttribute("srank",rank);
            rankAccounts = accountService.batchQuerySSRankAccount();
            rank = 0;
            for (int i = 0; i < rankAccounts.size(); i ++) {
                if (rankAccounts.get(i).getUserId() == userId) {
                    rank = i + 1;
                }
            }
            model.addAttribute("ssrank",rank);
            return new ModelAndView(resourceHolder.getStringValue("adminIndexUrl"));
		}
	}
	@RequestMapping("/sessiondelay")
	public ModelAndView sessionDelay(@RequestParam("type")String type){
		if(type.equals("0")){
			return new ModelAndView("session-delay");
		}else {
			return new ModelAndView("session-delay-index");
		}
	}

}
